package com.wu.service.weixin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wu.common.constant.Constant;
import com.wu.common.page.PageResult;
import com.wu.common.service.impl.BaseServiceImpl;
import com.wu.entity.sys.SysUserEntity;
import com.wu.entity.utils.DateUtils;
import com.wu.dao.weixin.dao.WeixinGzuserDao;
import com.wu.dao.weixin.vo.WeixinGzUserVo;
import com.wu.entity.weixin.po.WeixinGzuser;
import com.wu.service.sys.query.SysUserQuery;
import com.wu.service.sys.service.SysUserPostService;
import com.wu.service.sys.service.SysUserRoleService;
import com.wu.service.weixin.service.WxGzUserService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户管理
 *
 * @author wzd
 */
@Service
@AllArgsConstructor
public class WxGzUserServiceImpl extends BaseServiceImpl<WeixinGzuserDao, WeixinGzuser> implements WxGzUserService {
    private final SysUserRoleService sysUserRoleService;
    private final SysUserPostService sysUserPostService;

    @Override
    public PageResult<WeixinGzUserVo> page(SysUserQuery query) {
        // 查询参数
        Map<String, Object> params = getParams(query);

        // 分页查询
        IPage<WeixinGzuser> page = getPage(query);
        params.put(Constant.PAGE, page);
        // 数据列表
        List<WeixinGzuser> list = baseMapper.getList(params);
        List<WeixinGzUserVo> re = new ArrayList<>();
        list.forEach(i->{
            WeixinGzUserVo tag = new WeixinGzUserVo();
            BeanUtils.copyProperties(i,tag);
            tag.setAddress(i.getCountry()+i.getProvince()+i.getCity());
            tag.setSubscribetime(DateUtils.parseDate(i.getSubscribeTime(),"yyyy-mm-dd"));
            re.add(tag);
        });
        return new PageResult<WeixinGzUserVo>(re, page.getTotal());
    }

    @Override
    public List<WeixinGzuser> queryAll(String jwid) {

        return this.list(new QueryWrapper<WeixinGzuser>().eq("jwid", jwid));
    }


    private Map<String, Object> getParams(SysUserQuery query){
        Map<String, Object> params = new HashMap<>();
        params.put("username", query.getUsername());
        params.put("mobile", query.getMobile());
        params.put("gender", query.getGender());

        // 数据权限
        params.put(Constant.DATA_SCOPE, getDataScope("t1", null));

        return params;
    }

}
